﻿using HardcoreGame.Common.Players;
using HardcoreGame.Content.Buffs;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.DataStructures;
using Terraria.Enums;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
namespace HardcoreGame.Content.Items.Consumables.Drinks.Waters
{
    public class HallowRainwaterMug : ModItem
    {
        public int temperature = 15;
        public int timer = 0;
        public int holdTimer = 0;
        public override void SaveData(TagCompound tag)
        {
            tag["temperature"] = temperature;
        }
        public override void LoadData(TagCompound tag)
        {
            if (tag.ContainsKey("temperature"))
            {
                temperature = tag.GetInt("temperature");
            }
        }
        public override void SetDefaults()
        {
            Item.UseSound = SoundID.Item3;
            Item.useStyle = ItemUseStyleID.DrinkLiquid;
            Item.useTurn = true;
            Item.useAnimation = Item.useTime = 17;
            Item.consumable = true;
            Item.width = 18;
            Item.height = 18;
            Item.rare = ItemRarityID.White;
            Item.maxStack = Item.CommonMaxStack;
            Item.holdStyle = 1;
            Item.SetShopValues(ItemRarityColor.White0, 100);
            Item.value = Item.buyPrice(0, 0, 20);
        }
        public override void UpdateInventory(Player player)
        {
            timer++;
            holdTimer++;
            int changeRate = 0;
            if (player.ZoneSnow)
            {
                changeRate = -1;
            }
            else if (player.ZoneDesert)
            {
                if (temperature < 35)
                {
                    changeRate = 1;
                }
            }
            if (IsNearHeatSource(player))
            {
                if (temperature < 100)
                {
                    changeRate = 1;
                }
            }
            if (player.HeldItem.type == Type)
            {
                if (holdTimer > 3600)
                {
                    if (changeRate > 0)
                    {
                        changeRate += 1;
                    }
                    else if (changeRate < 0)
                    {
                        changeRate -= 1;
                    }
                }
            }
            else
            {
                holdTimer = 0;
            }
            if (timer % 360 == 0)
            {
                temperature += changeRate;
                if (player.ZoneSnow)
                {
                    if (temperature < -10)
                    {
                        temperature = -10;
                    }
                }
            }
        }
        private static bool IsNearHeatSource(Player player)
        {
            int searchRange = 10;
            int startX = (int)(player.position.X / 16) - searchRange;
            int startY = (int)(player.position.Y / 16) - searchRange;
            int endX = (int)(player.position.X / 16) + searchRange;
            int endY = (int)(player.position.Y / 16) + searchRange;
            for (int x = startX; x < endX; x++)
            {
                for (int y = startY; y < endY; y++)
                {
                    if (WorldGen.InWorld(x, y))
                    {
                        Tile tile = Main.tile[x, y];
                        if (tile.HasTile && (tile.TileType == TileID.Campfire || tile.TileType == TileID.Furnaces))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
        public override void ModifyTooltips(System.Collections.Generic.List<TooltipLine> tooltips)
        {
            TooltipLine nameLine = tooltips.Find(x => x.Name == "ItemName");
            if (nameLine != null)
            {
                nameLine.Text += $" {temperature}°C";
            }
        }
        public override bool CanUseItem(Player player)
        {
            player.QuickSpawnItem(player.GetSource_ItemUse(Item), ItemID.Mug, 1);
            Item.stack--;
            player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            player.AddBuff(ModContent.BuffType<WaterPollution>(), 3600);
            return true;
        }
        public override void SetStaticDefaults()
        {
            Item.ResearchUnlockCount = 20;
            Main.RegisterItemAnimation(Type, new DrawAnimationVertical(int.MaxValue, 3));
            ItemID.Sets.DrinkParticleColors[Item.type] = [new Color(203, 112, 218), new Color(164, 112, 218), new Color(124, 112, 218)];
        }
        public override void HoldStyle(Player player, Rectangle heldItemFrame)
        {
            int direction = player.direction;
            float verticalOffset = 13f;
            float horizontalOffset = -2f;
            player.itemLocation.X = player.MountedCenter.X + direction * (player.width / 2 + horizontalOffset);
            player.itemLocation.Y = player.MountedCenter.Y + verticalOffset;
        }
    }
}
